/**
 * 
 */
package org.codesketch.collector.common;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.codesketch.collector.domain.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Component;

/**
 * @author Quirno Brizi
 *
 */
@Component
public class XmlMarshaler {

	@Autowired
	private transient Jaxb2Marshaller marshaler;
	
	public String marshal(final Record record) {
		StringWriter writer = new StringWriter();
		marshaler.marshal(record, new StreamResult(writer));
		return writer.toString();
	}
	
	public Record unmarshal(final String record) {
		return (Record) marshaler.unmarshal(new StreamSource(new StringReader(record)));
	}
}
